$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/bmessagedict/list',
        datatype: "json",
        colModel: [
            {
                label: 'id', name: 'id', index: 'id', align: "center", width: 40, key: true,
                formatter: function (value, options, rowData) {
                    return "<a onclick=vm.editInfo(" + value + ")>" + value + "</a>";
                }
            },
            {label: '消息key', name: 'dictKey', index: 'dict_key', align: "center", width: 50},
            {
                label: '消息类型', name: 'dictType', index: 'dict_type', align: "center", width: 50,
                formatter: function (value, options, rowData) {
                    let content = "";
                    if (value === 0) {
                        content = '普通推送';
                    } else if (value === 1) {
                        content = '自动回复推送';
                    }
                    return content;
                }
            },
            {label: '消息文本', name: 'dictVal', index: 'dict_val', align: "center", width: 160},
            {label: '消息链接', name: 'dictLinkName', index: 'dict_link_name', align: "center", width: 80},
            {label: '触发文字', name: 'dictText', index: 'dict_text', align: "center", width: 80},
            {
                label: '状态', name: 'status', index: 'status', align: "center", width: 40,
                formatter: function (value, options, rowData) {
                    var content = "";
                    if (value === 1) {
                        content = '正常';
                    } else {
                        content = "<p style='color: red'>" + '停用' + "</>";
                    }
                    return content;
                }
            },
            {label: '更新者', name: 'updateBy', index: 'update_by', align: "center", width: 50},
            {label: '消息备注', name: 'remark', index: 'remark', align: "center", width: 80},
        ],
        viewrecords: true,
        height: 520,
        rowNum: 15,
        rowList: [15, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        bMessageDict: {}
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.bMessageDict = {
                dictType: 0,
                status: 1,
            };
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.bMessageDict.id == null ? "sys/bmessagedict/save" : "sys/bmessagedict/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json;charset=UTF-8",
                dataType: "JSON",
                data: JSON.stringify(vm.bMessageDict),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/bmessagedict/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "sys/bmessagedict/info/" + id, function (r) {
                vm.bMessageDict = r.bMessageDict;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                page: page
            }).trigger("reloadGrid");
        },
        editInfo: function (ids, event) {
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(ids)
        },
        updateStatus: function (ids, event) {
            vm.bMessageDict = {};
            confirm('确定修改状态？', function () {
                vm.bMessageDict.status = event;
                vm.bMessageDict.id = ids;
                console.log(vm.bMessageDict)
                var url = "sys/bmessagedict/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.bMessageDict),
                    success: function (r) {
                        if (r.code === 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
    }
});
